﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZhiAo.Entities;
using ECI.SSO.BizEntity;
using System.Web;
using System.Data;
using ZhiAo.WebLib.Data;

namespace ZhiAo.Management.Data
{
    public class QueryData
    {
        public static List<SqStbprpB> GetSqstbprpBList(string functionId)
        {
            SqStbprpB sqStbprpB = new SqStbprpB();
            string strSql = "SELECT Yr_stcd,STCD,Stnm,Lgtd,Lttd FROM SQ_STBPRP_B WHERE Yr_stcd in (" + GetSiteIds(functionId) + ") AND Lgtd <> '' AND Lttd <> '' AND Lgtd is not null and lttd is not null";
            List<SqStbprpB> lstSqStbprpB = sqStbprpB.FetchEntities(strSql);
            return lstSqStbprpB;
        }

        public static string GetSiteIds(string functionId)
        {
            //List<Function> funtions = MenuInfo.GetFunctionsByRootId(functionId);
            StringBuilder sb = new StringBuilder();
            //foreach (var func in funtions)
            //{
            //    sb.Append("'" + func.Code + "',");
            //}
            //if (sb.Length > 0)
            //{
            //    sb.Remove(sb.Length - 1, 1);
            //}
            //else
            //{
            //    sb.Append("''");
            //}
            return sb.ToString();
        }

        public static List<DataRow> GetSqstbprpBDt(string functionId)
        {
            List<DataRow> result = new List<DataRow>();
            DataTable stations = HttpContext.Current.Cache["stations"] as DataTable;
            List<DataRow> functions = MenuInfo.GetFunctionsByRootId(functionId);
            foreach (DataRow station in stations.Rows)
            {
                foreach (DataRow function in functions)
                {
                    if (station["yr_stcd"].ToString() == function["code"].ToString())
                    {
                        result.Add(station);
                    }
                }
            }
            return result;
        }



        public static List<DataRow> GetChildrenStations(string functionId)
        {           
            //1. 获取当前用户的所有带站点信息的功能点
            DataTable stationsDt = GetFunctionTreeByRootId();
            //2. 从获取的所有功能点中筛选出functionId及functionId的所有子站点信息
            List<DataRow> childrenStations = MenuInfo.GetFunctionsOnlyLeaf(stationsDt, functionId);
            return childrenStations;
        }

        private static DataTable GetFunctionTreeByRootId()
        {
            string userId = UserInfo.CurrentUserInfo.User.ID;
            string sql = @"SELECT s.Yr_stcd,s.STCD,s.Stnm,s.Lgtd,s.Lttd ,f.functionid,f.parentid,f.name,f.code,f.url,f.showinmenu
                    FROM powerplat.dbo.SSO_FUNCTION f
                    left join
                    (select s.Yr_stcd,s.STCD,s.Stnm,s.Lgtd,s.Lttd from SQ_STBPRP_B s where Lgtd <> '' AND Lttd <> '' AND Lgtd is not null and lttd is not null) s
                    on s.yr_stcd=f.code
                    inner join powerplat.dbo.SSO_ENTITY2FUNC e
                    on f.code=e.functioncode 
                    WHERE e.entityid='{0}'";
            sql = string.Format(sql, userId);
            return DBHelper.GetDataTable(sql); 
        }
    }
}
